package com.lanou.renrenxin.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;

import java.lang.ref.SoftReference;

/**
 * Created by zy on 16/3/23.
 */
public class PrivicyActivity extends BaseActivity implements View.OnClickListener {
    private Button btnCreat, btnBack;
    private TextView textViewTitle;
    private EditText editTextPassword, editTextDetermine;
    private String password, determinePassword, pas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privicy);
        initView();
        setUP();

    }

    private void initView() {
        editTextPassword = (EditText) findViewById(R.id.privicy_password);
        editTextDetermine = (EditText) findViewById(R.id.privicy_determine);
        btnCreat = (Button) findViewById(R.id.btnCreat);
        findViewById(R.id.privicy_btn_gotoprivictlist).setOnClickListener(this);
        textViewTitle = (TextView) findViewById(R.id.txtTitle);
        btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
    }

    private void setUP() {
        btnCreat.setVisibility(View.GONE);
        btnBack.setVisibility(View.VISIBLE);
        textViewTitle.setText("私密聊");
        //取到之前存的密码
        SharedPreferences sharedPreferences = getSharedPreferences("password", MODE_PRIVATE);
        pas = sharedPreferences.getString("password", "");
        if (pas.length() != 0) {
            editTextDetermine.setVisibility(View.GONE);

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.privicy_btn_gotoprivictlist:
                if (pas.length() != 0) {
                    String input  = editTextPassword.getText().toString();
                    if (input.equals(pas)) {

                        //进入私密了界面
                        Intent intent = new Intent(this,DetailPrivicyActivity.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(this, "密码有误", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    password = editTextPassword.getText().toString();
                    determinePassword = editTextDetermine.getText().toString();
                    if (password.equals(determinePassword)) {
                        savePassword(password);
                        Toast.makeText(this, "密码设置成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                        password = null;
                        determinePassword = null;
                    }
                }
                    break;
            case R.id.btnBack:
                finish();
                break;
        }
    }

    //存贮密码
    private void savePassword(String password) {
        SharedPreferences sharedPreferences = getSharedPreferences("password", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("password", password);
        //保存
        editor.commit();
    }
}
